// utils/date.ts
import type { Dayjs } from 'dayjs'

/**
 * 获取两个日期之间的天数差
 */
export const getDaysBetween = (startDate: string | Dayjs, endDate: string | Dayjs): number => {
  const start = new Date(startDate)
  const end = new Date(endDate)
  return Math.floor((end.getTime() - start.getTime()) / (1000 * 60 * 60 * 24))
}

/**
 * 检查日期是否在范围内
 */
export const isDateInRange = (
  date: string | Dayjs,
  range: [string | Dayjs, string | Dayjs]
): boolean => {
  const target = new Date(date)
  const start = new Date(range[0])
  const end = new Date(range[1])
  return target >= start && target <= end
}

/**
 * 格式化日期为友好显示
 */
export const formatToFriendlyDate = (date: string | Dayjs): string => {
  const now = new Date()
  const target = new Date(date)
  const diffDays = Math.floor((now.getTime() - target.getTime()) / (1000 * 60 * 60 * 24))

  if (diffDays === 0) return '今天'
  if (diffDays === 1) return '昨天'
  if (diffDays === 2) return '前天'
  if (diffDays < 7) return `${diffDays}天前`

  return target.toLocaleDateString()
}
